﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Web;

namespace SqueezeboxBridge
{
    public class Slim
    {
        private string _squeezeCenter;

        private List<SlimPlayer> _players;

        public List<SlimPlayer> Players
        {
            get
            {
                if (_players == null)
                {
                    _players = new List<SlimPlayer>();
                    string[] players = RetrievePlayers();
                    for (int i = 0; i < players.Length; i++)
                    {
                        SlimPlayer player = new SlimPlayer();
                        player.Index = i;
                        player.Id = players[i];
                        player.Name = RetrievePlayerName(i);
                        _players.Add(player);
                    }
                }
                return _players;
            }
        }


        public Slim(string squeezeCenter)
        {
            _squeezeCenter = squeezeCenter;
        }

        public string[] RetrievePlayers()
        {
            // get nr of players
            string countResult = ExecuteCommand("player count ?\n");
            countResult = countResult.Remove(0, 13);
            int playerCount = Int16.Parse(countResult);

            // now retrieve all players
            List<string> players = new List<string>();
            for (int i = 0; i < playerCount; i++)
            {
                string idResult = ExecuteCommand("player id " + i + " ?\n");
                idResult = idResult.Remove(0, i.ToString().Length + 11).Trim();
                players.Add(idResult);
            }
            return players.ToArray();
        }

        public string RetrievePlayerName(int id)
        {
            string result = ExecuteCommand("player name " + id + " ?\n");
            return result.Remove(0, id.ToString().Length + 13).Trim();
        }

        public string ExecuteCommand(string cmd)
        {
            TcpClient tcp = new TcpClient(_squeezeCenter, 9090);
            NetworkStream stream = tcp.GetStream();

            // execute command
            byte[] data = Encoding.ASCII.GetBytes(cmd);
            stream.Write(data, 0, data.Length);

            // response?
            data = new Byte[256];

            // String to store the response ASCII representation.
            String responseData = String.Empty;

            // Read the first batch of the TcpServer response bytes.
            Int32 bytes = stream.Read(data, 0, data.Length);
            responseData = HttpUtility.UrlDecode(Encoding.ASCII.GetString(data, 0, bytes));

            stream.Close();
            tcp.Close();

            return responseData;
        }

        public void PlayUri(string playerId, string uri)
        {
            string cmd = playerId + " playlist play " + uri + "\n";
            ExecuteCommand(cmd);        
        }
    }
}
